package com.action.school;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;

import com.action.base.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.pageModel.Json;
import com.pageModel.Student;
import com.service.school.StudentServiceI;

/**
 * 学生ACTION
 * 
 * @author chenq
 * 
 */

@Namespace("/school")
@Action(value = "studentAction")
public class StudentAction extends BaseAction implements ModelDriven<Student> {

	Student student = new Student();

	@Override
	public Student getModel() {
		return student;
	}

	private StudentServiceI studentService;

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public StudentServiceI getStudentService() {
		return studentService;
	}

	public void setStudentService(StudentServiceI studentService) {
		this.studentService = studentService;
	}

	public void datagrid() {
		super.writeJson(studentService.datagrid(student));
	}

	public void add() {
		String json = null;
		Map<String, Object> m = new HashMap<String, Object>();
		try {
			boolean state = studentService.add(student);
			if (state) {
				m.put("success", true);
				m.put("msg", "添加成功!");
			} else {
				m.put("success", false);
				m.put("msg", "该学生已存在!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			m.put("success", false);
			m.put("msg", "添加失败！");
			System.out.print(e.getMessage());
		}
		super.writeJson(m);
	}

	public void remove() {
		studentService.remove(student.getIds());
		Json j = new Json();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}
	
	public void edit() {
		Json j=new Json();
		try {
			studentService.edit(student);
			j.setSuccess(true);
			j.setMsg("修改成功！");
		}
		catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
			j.setMsg("添加失败！");
			System.out.print(e.getMessage());
		}
		super.writeJson(j);
	}
//	public void editSpecial() {
//		studentService.editSpecial(student);
//		Json j=new Json();
//		j.setSuccess(true);
//		j.setMsg("修改成功！");
//		super.writeJson(j);
//	}
	
}
